package it.intecs.pisa.smarcos.services;

import it.intecs.pisa.smarcos.receivers.WifiScanResultsBroadcastReceiver;

import java.util.List;

import android.app.DownloadManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;


/* 1) setWifiEnabled(true) abilita il wifi ma non effettua lo scan delle reti: nessun intent a WifiScanResultsBroadcastReceiver se 
 *    non eseguo anche startScan. Pero', sullo smartphone, si accende il simbolo wifi anche eseguendo solo la setWifiEnabled(true).
 *    Ma, anche se il simbolo wifi e' acceso non arrivano intent di sistema al broadcast receiver inerenti il wifi. Boh.  
 * 2) gli intent lanciati dal sistema e relativi al servizio wifi sono: 
 *    SCAN_RESULTS_AVAILABLE_ACTION, 
 *    NETWORK_STATE_CHANGED_ACTION,
 * 	  SUPPLICANT_CONNECTION_CHANGE_ACTION, 
 *    SUPPLICANT_STATE_CHANGED_ACTION
 *    WIFI_STATE_CHANGED_ACTION 
 * 3) Una volta eseguita la mWifiManager.startScan() per cercare le reti wifi non esiste la stop. L'unico modo per
 *    "smettere" di cercare reti e' fare la unregister del BroadcastRceiver
 */

public class WirelessDiscoveryService extends Service {
	private static final String TAG = WirelessDiscoveryService.class.getSimpleName();
	public static final String ISTART_SRV = "it.intecs.pisa.smarcos.services.WirelessDiscoveryService";
	public static String WIFI_SSID="gruppospazio";
	boolean scanning = false;
	protected static final long FIXED_RATE=1000*60;
	protected WifiScanResultsBroadcastReceiver scanBR=null;
	protected WifiManager mWifiManager = null;
	IntentFilter connectionFilter;
	ConnectionStatusReceiver connectionReceiver;
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		Log.d(TAG,"OnCreate");
	
		mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
		List<WifiConfiguration> confNetworks = mWifiManager.getConfiguredNetworks();
		for(WifiConfiguration net:confNetworks)
			Log.d(TAG, net.toString());

		connectionFilter=new IntentFilter();
		connectionFilter.addAction(ConnectionStatusReceiver.INTENT_SHOW);
		connectionFilter.addCategory(Intent.CATEGORY_DEFAULT);
		connectionReceiver=new ConnectionStatusReceiver();
		registerReceiver(connectionReceiver, connectionFilter);
		
		scanBR=new WifiScanResultsBroadcastReceiver();
		IntentFilter scanFilter=new IntentFilter();
		scanFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
		scanFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
		scanFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
		scanFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
		scanFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
		scanFilter.addCategory(Intent.CATEGORY_DEFAULT);
		this.registerReceiver(scanBR, scanFilter);
		
		mWifiManager.setWifiEnabled(true);
		
		startScan();	
	}
	
	
	
	@Override
	public void onDestroy() {
		super.onDestroy();

		scanBR.setConnected();
		
		this.unregisterReceiver(scanBR);
		this.unregisterReceiver(connectionReceiver);
				
		mWifiManager.disconnect();
		mWifiManager.setWifiEnabled(false);
		Log.d(TAG, "SMARCOS network disconnected");
	}
	
	protected void startScan() {
		Log.d(TAG,"startScan");
		
		if (!scanning){
			if (mWifiManager.startScan() == false)
				Log.d(TAG,"Wifi scan not started");
			else
				Log.d(TAG,"Wifi scan started");
			scanning = true;
		}
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	public class ConnectionStatusReceiver extends BroadcastReceiver {

		public static final String INTENT_SHOW="it.intecs.pisa.smarcos.services.ConnectionStatusReceiver.SHOW";
		private final String TAG = ConnectionStatusReceiver.class.getSimpleName();

		@Override
		public void onReceive(Context arg0, Intent intent) {

			String connStatus = intent.getStringExtra("ConnectionStatus");
			Log.d(TAG, "OnReceive - " + connStatus);

			if (connStatus.equals("CONNECTED")) { 

				
			}

			if (connStatus.equals("DISCONNECTED")) { 
		
			}

		}		
	}
	


}
